/* GUI.C */

/*
	Copyright (C) 2010 by Lesnikov Andrey <ozkriff@gmail.com>
	This file is part of ozkriff_game_2.

	ozkriff_game_2 is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	ozkriff_game_2 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with ozkriff_game_2.  If not, see <http://www.gnu.org/licenses/>.

	See the COPYING file for more details.
*/

#include <stdbool.h>
#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_opengl.h"

#include "defines.h"
#include "gui.h"

extern int SCR_WIDTH;
extern int SCR_HEIGHT;
extern uint PLAYER;


int BUTTONS_COUNT = 0; //число задействованных кнопок
int SELECTED_BUTTON; //индекс выбранной кнопки
gui_button BUTTONS[100]; //массив кнопок

TTF_Font* font;


void
draw_button_for_picking(gui_button b)
{
	glPushMatrix();
	{
		glTranslatef(b.x, b.y, 0);
		glBegin(GL_QUADS);
		{
			glVertex3d(0, 0, 0);
			glVertex3d(b.w, 0, 0);
			glVertex3d(b.w, b.h, 0);
			glVertex3d(0, b.h, 0);
		}
		glEnd();
	}
	glPopMatrix();
}


void
draw_interface_for_picking()
{
	glDisable(GL_DEPTH_TEST);
	
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(0, SCR_WIDTH, SCR_HEIGHT, 0, 0, 1);
	
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	{
		glLoadIdentity();
		
		int i;
		for (i = 0; i <BUTTONS_COUNT; i++) {
			glColor3ub(0, 0, i+2);
			draw_button_for_picking(BUTTONS[i]);
		}
	}
	glPopMatrix();
	
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	
	glMatrixMode(GL_MODELVIEW);
	
	glEnable(GL_DEPTH_TEST);
}



unsigned
generate_texture_from_str(char* Text, int *w, int *h)
{
	SDL_Color Color = {255, 255, 255, 0};
	SDL_Surface *txt = TTF_RenderUTF8_Blended(font, Text, Color);
	unsigned id = 0; // id текстуры
	
	glGenTextures(1, &id); /*Generate an OpenGL 2D texture from the SDL_Surface*.*/
	glBindTexture(GL_TEXTURE_2D, id); // из-за отсутствия этой строки было потрачено много времени
	
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, txt->w, txt->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, txt->pixels);
	*w = txt->w;
	*h = txt->h;
	SDL_FreeSurface(txt);
	
	return(id);
}



void
change_button_text (gui_button* b, char* text)
{
	glDeleteTextures(1, &(b->textureID));
	b->textureID = generate_texture_from_str(text, &(b->texture_w), &(b->texture_h));
}



void
change_button_text_by_name (char* name, char* text)
{
	int i;
	for(i=0; i< BUTTONS_COUNT; i++){
		if(!strcmp(BUTTONS[i].name, name)){
			change_button_text(&(BUTTONS[i]), text);
			break;
		}
	}
}


bool
add_button(int x, int y, int W, int H, char* name, char *text)
{
	unsigned textureID;
	int w, h;
	textureID = generate_texture_from_str(text, &w, &h);
	
	if (textureID == 0)
		exit(1);
		
	gui_button b = {name, x, y, W, H, w, h, textureID, text};
	
	BUTTONS[BUTTONS_COUNT] = b;
	BUTTONS_COUNT++;
	
	return(true);
}




void
draw_button(gui_button b)
{
	glPushMatrix();
	{
		glTranslatef(b.x, b.y, 0);
		
		glColor4f(0.3, 0.3, 0.3, 0.8);
		glBegin(GL_QUADS);
		{
			glVertex2d(0,	b.h);
			glVertex2d(b.w, b.h);
			glVertex2d(b.w, 0);
			glVertex2d(0,	0);
		}
		glEnd();
		
		glColor4f(1, 1, 1, 0.8);
		//glScalef(0.3, 0.3, 0.3);
		
		//RENDER TEXT
		{
			glEnable(GL_TEXTURE_2D);
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
			glBindTexture(GL_TEXTURE_2D, b.textureID);
			
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			
			glBegin(GL_QUADS);
			{
				//glTexCoord2d(0, 0); glVertex2d(0, 0);
				//glTexCoord2d(1, 0); glVertex2d(b.texture_w, 0);
				//glTexCoord2d(1, 1); glVertex2d(b.texture_w, b.texture_h);
				//glTexCoord2d(0, 1); glVertex2d(0, b.texture_h);

				glTexCoord2d(0, 1); glVertex2d(0, b.texture_h);
				glTexCoord2d(1, 1); glVertex2d(b.texture_w, b.texture_h);
				glTexCoord2d(1, 0); glVertex2d(b.texture_w, 0);
				glTexCoord2d(0, 0); glVertex2d(0, 0);
			}
			glEnd();
			
			glDisable(GL_TEXTURE_2D);
		}
	}
	glPopMatrix();
}



void
draw_interface()
{
	glDisable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);
	
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(0, SCR_WIDTH, SCR_HEIGHT, 0, 0, 1);
	
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	{
		glLoadIdentity();
		
		int i;
		for (i = 0; i < BUTTONS_COUNT; i++)
			draw_button(BUTTONS[i]);
	}
	glPopMatrix();
	
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	
	glMatrixMode(GL_MODELVIEW);
	
	glDisable(GL_BLEND);
	glEnable(GL_DEPTH_TEST);
}




void
init_interface()
{
	if (TTF_Init() == -1) {
		printf("Unable to initialize SDL_ttf: %s \n", TTF_GetError());
		exit(1);
	}
	//font = TTF_OpenFont("font.ttf", 24);
	font = TTF_OpenFont("../font.ttf", 18);
	if (font == NULL) {
		printf("Unable to initialize font: %s \n", TTF_GetError());
		exit(1);
	}
	
	//add_button(0, 0, 80, 25, "button", "кнопка");
	if(PLAYER == 1)
		add_button(0, 0, 80, 25, "player identifier", "красный");
	if(PLAYER == 2)
		add_button(0, 0, 80, 25, "player identifier", "синий");
	//add_button(0, 0, 80, 25, "player identifier", "красный");
	//add_button(300, 300, 80, 25, "test button 1", "butt 1");
	//add_button(350, 350, 80, 25, "test button 2", "butt 2");
	
	add_button(0, 25, 80, 25, "who_active", "ход противника");
}


